package com.ddwl.gate.service;

import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.constant.Constant;
import com.ddwl.common.exception.ParamException;
import com.ddwl.common.util.BeanConverUtils;
import com.ddwl.common.util.MySqlUtil;
import com.ddwl.gate.dao.SupportDao;
import com.ddwl.gate.model.Support;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.gate.SupportBo;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.gate.SupportVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author wenbin
 * @Date 2020/10/14 14:23
 */
@Service
@RefreshScope
@Slf4j
public class SupportService {
    @Resource
    SupportDao supportDao;

    public void add(SupportBo bo) {
        Support exist = getByName(bo.getTitle());
        ParamException.isNotNull(exist,"已经存在该名称的栏目");
        Support entity = BeanConverUtils.convertBean(bo, Support.class);
        entity.setEffect(BaseEnum.StatusEnum.ENABLE.getCode());
        supportDao.insert(entity);
    }


    public void update(SupportBo bo) {
        Support exist = getByName(bo.getTitle());
        if (null != exist) {
            ParamException.isFalse((exist.getId().equals(bo.getId())), "已经存在该名称的栏目");
        }
        Support entity = BeanConverUtils.convertBean(bo, Support.class);
        supportDao.updateByPrimaryKeySelective(entity);
    }

    private Support getByName(String title){
        Support entity = new Support();
        entity.setTitle(title);
        entity.setDflag(BaseEnum.DflagEnum.NOT_DELETE.getCode());
        return supportDao.selectOne(entity);
    }

    public SupportVo getForConsole(String id) {
        return BeanConverUtils.convertBean(supportDao.selectByPrimaryKey(id), SupportVo.class);
    }

    public SupportVo get(String id) {
        Support entity = new Support();
        entity.setId(id);
        entity.setEffect(BaseEnum.StatusEnum.ENABLE.getCode());
        return BeanConverUtils.convertBean(supportDao.selectOne(entity), SupportVo.class);
    }


    public PageVo<SupportVo> page(PageBo<SupportBo> pageBo) {
        PageHelper.startPage(pageBo.getPage(), pageBo.getPageSize(), MySqlUtil.orderBy(pageBo.getSort(), pageBo.isAsc()));
        PageInfo<Support> info = new PageInfo<>(supportDao.getList(pageBo.getParam()));
        List<SupportVo> list = BeanConverUtils.convertBeanList(info.getList(), SupportVo.class);
        return new PageVo<>(info.getTotal(), pageBo.getPage(), pageBo.getPageSize(), list);
    }

    public void effect(SupportBo bo) {
        Support entity = new Support();
        entity.setEffect(bo.getEffect());
        entity.setId(bo.getId());
        supportDao.updateByPrimaryKeySelective(entity);
    }


    public void delete(String id) {
        supportDao.deleteById(id);
    }
}
